package com.pzl.mylistview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.logging.Logger;

/**
 * 自定义重写listview
 * Created by zl.peng on 2016-03-07.
 */
public class MyListView extends ViewGroup {
    private static final String TAG = "MyListView";
    private Adapter mAdapter;
    private int mDividorStroke = 1;
    private int mDividorColor = Color.WHITE;
    private Context mContext;
    private float mYlast;
    private float mYnow;
    private View mFirstItemView;
    private View mLastItemView;
    private int mContentHeight;
    private int mFinalMeasureHeight;

    public MyListView(Context context) {
        this(context, null);
    }

    public MyListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        Log.i(TAG, "MyListView(x,x)");
        mContext = context;
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private View getDivider() {
        View mDividor = new View(mContext);
        mDividor.setBackgroundColor(mDividorColor);
        ViewGroup.LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, mDividorStroke);
        mDividor.setLayoutParams(layoutParams);
        return mDividor;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.i(TAG, "onMeasure");

        if (mAdapter == null) throw new IllegalStateException("请设置适配器！");

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        mFinalMeasureHeight = heightSize;

        int width;
        int height;

        /**
         * 测量子view
         */
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        /**
         * 使用同一个item，相同宽度要求
         */
        width = getChildAt(0).getMeasuredWidth();

        // int childCount = getChildCount();
        int itemCount = mAdapter.getCount();

        /**
         * 暂定相同item，高度一样
         */
        height = (getChildAt(0).getMeasuredHeight() * itemCount) + (getChildAt(1).getMeasuredHeight() * (itemCount - 1));
        mContentHeight = height;

        Log.i(TAG, "widthMSpec=" + widthSize + ",heightMSpec=" + heightSize);
        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width, heightMode == MeasureSpec.EXACTLY ? heightSize : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.i(TAG, "onLayout=" + "l=" + l + ",t=" + t + ",r=" + r + ",b=" + b);
        int itemHeight = getChildAt(0).getMeasuredHeight();
        for (int j = 0; j < mAdapter.getCount() * 2 - 1; j++) {
            View child = getChildAt(j);
            if (j % 2 == 0) {
                child.layout(0, (child.getMeasuredHeight() + mDividorStroke) * j / 2, child.getMeasuredWidth(), (child.getMeasuredHeight() + mDividorStroke) * (j / 2 + 1));
            } else {
                child.layout(0, itemHeight * ((j + 1) / 2) + mDividorStroke * ((j + 1) / 2 - 1), child.getMeasuredWidth(), itemHeight * ((j + 1) / 2) + mDividorStroke * ((j + 1) / 2 - 1) + mDividorStroke);
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Log.i(TAG, "onTouchEvent");
        Log.i(TAG, "getScrollY=" + getScrollY());
        Log.i(TAG, "getHeight=" + getHeight());
        Log.i(TAG, "mFinalMeasureHeight=" + mFinalMeasureHeight);
        Log.i(TAG, "mContentHeight=" + mContentHeight);
        int ACTION = event.getAction();
        switch (ACTION) {
            case MotionEvent.ACTION_DOWN:
                mYnow = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                mYlast = mYnow;
                mYnow = event.getY();
                float dy = mYlast - mYnow;
                if (getScrollY() < 0 || getScrollY() + dy < 0) {
                    scrollTo(0, 0);
                    break;
                }
                if (getScrollY() + mFinalMeasureHeight > mContentHeight || getScrollY() + dy + mFinalMeasureHeight > mContentHeight) {
                    scrollTo(0, mContentHeight - mFinalMeasureHeight);
                    break;
                }
                scrollBy(0, (int) dy);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        //requestLayout();
        return true;
    }

    /**
     * 设置适配器
     *
     * @param adapter
     */
    public void setAdapter(Adapter adapter) {
        Log.i(TAG, "setAdapter");

        mAdapter = adapter;
        update();
    }

    /**
     * 更新数据源后刷新 mylistview
     */
    public void update() {
        removeAllViews();
        for (int i = 0; i < mAdapter.getCount(); i++) {
            View view = mAdapter.getView(i, null, this);
            if (i == 0) {
                mFirstItemView = view;
            }
            if (i == mAdapter.getCount() - 1) {
                mLastItemView = view;
            }
            addView(view);
            if (i != mAdapter.getCount() - 1) {
                addView(getDivider());
            }
        }
    }

}
